﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Connection;
using Connection.Presenter;
using Connection.IView;
using System.Windows.Threading;

namespace RemoChat
{
    /// <summary>
    /// Interaction logic for WinChat.xaml
    /// </summary>
    public partial class WinChat : Window, IWinChatView
    {
        private string URL;

        public WinChat()
        {
            InitializeComponent();
        }

        public WinChat(string url)
        {
            InitializeComponent();
            URL = url;
        }

        public WinChat(string url, string tousername)
        {
            InitializeComponent();
            URL = url;
            this.ToUsername = tousername;
        }

        public WinChat(string url, string tousername, string fromusername)
        {
            InitializeComponent();
            URL = url;
            this.ToUsername = tousername;
            this.FromUsername = fromusername;

            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                ChatPresenter chatPresenter = (ChatPresenter)Activator.GetObject(typeof(ChatPresenter), URL + "PRESENTER/CHATPRESENTER_OBJECT");
                this.MessageDisplay = chatPresenter.ShowChat(this.FromUsername, this.ToUsername);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public string MessageSend
        {
            get { return txtChatNow.Text; }
            set { txtChatNow.Text = value; }
        }

        public string ToUsername
        {
            get { return lbToUsername.Content.ToString(); }
            set { lbToUsername.Content = value; }
        }

        public string FromUsername
        {
            get { return lbFromUsername.Content.ToString(); ; }
            set { lbFromUsername.Content = value; }
        }


        public string MessageDisplay
        {
            get { return txtShowAllMessage.Text; }
            set { txtShowAllMessage.Text = value; }
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            ChatPresenter chatPresenter = (ChatPresenter)Activator.GetObject(typeof(ChatPresenter), URL + "PRESENTER/CHATPRESENTER_OBJECT");
            int i = chatPresenter.Test();
            chatPresenter.StartConversation(this.FromUsername, this.ToUsername);
            chatPresenter.AddNewMessage(this.MessageSend, this.FromUsername);
            this.MessageSend = "";
        }

        private void txtChatNow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnSend_Click(sender, e);
        }
    }
}
